﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace GetThis_Server.Users
{
    internal static class DataHelper
    {
        private static object m_lock = new object();

        private static string m_dataPath;

        internal static bool TryLoad(this Data data, string dataPath)
        {
            Contract.Requires<ArgumentNullException>(data != null);
            Contract.Requires<ArgumentNullException>(!String.IsNullOrWhiteSpace(dataPath));

            lock (m_lock)
            {
                try
                {
                    data.ReadXml(dataPath);
                    m_dataPath = dataPath;
                }
                catch (SystemException)
                {
                    return false;
                }
            }
            return true;
        }

        internal static bool TrySave(this Data data)
        {
            Contract.Requires<ArgumentNullException>(data != null);

            lock (m_lock)
            {
                try
                {
                    data.WriteXml(m_dataPath);
                }
                catch (SecurityException)
                {
                    return false;
                }
            }
            return true;
        }
    }
}
